import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 35326
 * 2021/3/26 9:26
 * 时间和日期API
 */
public class DateAndTimeAPITest {
	@Test
	public void method1(){
		//System.currentTimeMillis()
		System.out.println(System.currentTimeMillis());
		//java.util.Date
		//---|java.sql.Date
		/**
		 *
		 * 构造器：
		 * Date()
		 * Date(long TimeMillis)
		 *
		 * 方法：
		 * toString():显示当前的日期，年 月 日 时 分 秒
		 * getTime():返回当前Date类对应的毫秒数，也就是时间戳
		 *
		 */
		Date date = new Date();
		System.out.println(date);
		Date sqlDate = new java.sql.Date(date.getTime());
		System.out.println(sqlDate);
		/**
		 * SimpleDateFormat的使用：格式化和解析。
		 */
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		/**
		 * Calendar:日历类
		 * 1. 实例化
		 * 	1. 创建其子类的对象
		 * 	2.调用其静态方法getInstance()
		 * 2.常用方法
		 * 	get()
		 * 	set()
		 * 	add()
		 * 	getTime()
		 * 	setTime()
		 */
		Calendar instance = Calendar.getInstance();

		int i = instance.get(Calendar.DAY_OF_MONTH);
		System.out.println(i);
		System.out.println(instance.get(Calendar.DAY_OF_YEAR));
		Date time = instance.getTime();
		System.out.println(time);
		int i1 = instance.get(Calendar.MONTH);
		System.out.println(i1);

	}

	@Test
	/**
	 * jdk1.8 中的时间日期API
	 */
	public void test2(){
		/**
		 * LocalDate
		 * LocalTime
		 * LocalDateTime
		 * now()获取当前的日期和时间
		 */
		LocalDate now = LocalDate.now();
		LocalTime now1 = LocalTime.now();
		LocalDateTime now2 = LocalDateTime.now();
		System.out.println(now);
		System.out.println(now1);
		System.out.println(now2);

		/**
		 * of():获取指定的时间和日期
		 */
		LocalDateTime of = LocalDateTime.of(2021, 3, 26, 11, 11, 10);
		System.out.println(of);

		/**
		 * getXxx()相关操作
		 */

		int dayOfMonth = of.getDayOfMonth();
		System.out.println(dayOfMonth);
		int dayOfYear = of.getDayOfYear();
		System.out.println(dayOfYear);

		/**
		 * with()体现的是不可变性
		 */
		LocalDateTime localDateTime = of.withHour(1);
		System.out.println(of);
		System.out.println(localDateTime);
		System.out.println(LocalDate.of(2021, 3, 14));

		/**
		 * plusXxx()方法
		 *
		 */
		LocalDateTime localDateTime1 = of.plusDays(1);
		LocalDateTime localDateTime2 = of.minusDays(1);

	}

	@Test
	/**
	 * Instant的使用
	 */
	public void testInstant(){
		Instant now = Instant.now();
		OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
		System.out.println(offsetDateTime);

		long l = now.toEpochMilli();
		System.out.println(l);
		System.out.println(System.currentTimeMillis());

		Instant instant = Instant.ofEpochMilli(l);
		System.out.println(instant);
	}

	@Test
	/**
	 * DateTimeFormatter的使用
	 */
	public void testDateTimeFormatter(){
		//方法一：预定义的相关格式
		DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		LocalDateTime now = LocalDateTime.now();
		String format = isoLocalDateTime.format(now);
		System.out.println(format);
		TemporalAccessor parse = isoLocalDateTime.parse("2021-03-26T14:40:04.514");
		System.out.println(parse);

		//方法二：本地化相关的格式
		//ofLocalizedDate()   ofLocalizedDateTime()
		//FormatStyle.LONG/FormatStyle.MEDIUM
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
		System.out.println(dateTimeFormatter.format(LocalDate.now()));

		//方法三：自定义的相关格式
		//ofPattern("yyyy-MM-dd hh:mm:ss")

	}
	@Test
	public void testInstant2(){
	     Runnable runnable = ()->{while(true){

			 System.out.println(1);
		 }};
	     Thread t = new Thread(runnable);
		t.start();
	}

	@Test
	public void testThread(){
	    MyThread m = new MyThread();
	    Thread t = new Thread(m);
	    t.start();
	}

	public static void main(String[] args) {
		Runnable runnable = ()->{while(true){

			System.out.println(1);
		}};
		Thread t = new Thread(runnable);
		t.start();
	}
}

class MyThread implements Runnable{
	@Override
	public void run() {
		while(true){
			System.out.println(1);
		}
	}
}
